﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace PivotViewerOffice365.PivotViewerWP
{
    public class EditorUIPivotViewer : EditorPart
    {
        CheckBox chkHideTitleBar;
        CheckBox chkHideFilterPanel;
        CheckBox chkHideInfoPanel;

        TextBox txtAlphaTitle;
        TextBox txtRedTitle;
        TextBox txtGreenTitle;
        TextBox txtBlueTitle;
        TextBox txtUrlTitle;



        TextBox txtAlphaFilter;
        TextBox txtRedFilter;
        TextBox txtGreenFilter;
        TextBox txtBlueFilter;
        TextBox txtUrlFilter;


        TextBox txtAlphaInfo;
        TextBox txtRedInfo;
        TextBox txtGreenInfo;
        TextBox txtBlueInfo;
        TextBox txtUrlInfo;


        TextBox txtAlphaContent;
        TextBox txtRedContent;
        TextBox txtGreenContent;
        TextBox txtBlueContent;
        TextBox txtUrlContent;

        DropDownList ddlStretchContent;
        DropDownList ddlStretchInfo;
        DropDownList ddlStretchTitle;
        DropDownList ddlStretchFilter;

        protected override void CreateChildControls()
        {
            this.Title = "Edit the Pivot User Interface";
            Table tbl = new Table();
            this.Controls.Add(tbl);

            TableRow tr = new TableRow();
            tbl.Rows.Add(tr);

            TableCell tc = new TableCell();

            tr.Cells.Add(tc);
            tc.Controls.Add(new LiteralControl("<h3>Hide title bar</h3>"));
            tc.Wrap = false;

            tc = new TableCell();
            tr.Cells.Add(tc);
            chkHideTitleBar = new CheckBox();
            tc.Controls.Add(chkHideTitleBar);


            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Controls.Add(new LiteralControl("<h3>Hide filter panel</h3>"));
            tc.Wrap = false;

            tc = new TableCell();
            tr.Cells.Add(tc);
            chkHideFilterPanel = new CheckBox();
            tc.Controls.Add(chkHideFilterPanel);

            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Controls.Add(new LiteralControl("<h3>Hide info panel</h3>"));
            tc.Wrap = false;


            tc = new TableCell();
            tr.Cells.Add(tc);
            chkHideInfoPanel = new CheckBox();
            tc.Controls.Add(chkHideInfoPanel);

            tbl = new Table();
            this.Controls.Add(tbl);
            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.ColumnSpan = 2;
            tc.Controls.Add(new LiteralControl("<h3>Background for title bar</h3>"));



            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Width = new Unit(1);
            tc.Controls.Add(new LiteralControl("Alpha\t:"));


            tc = new TableCell();
            tr.Cells.Add(tc);
            txtAlphaTitle = new TextBox();
            txtAlphaTitle.MaxLength = 3;
            txtAlphaTitle.Width = new Unit(30);
            tc.Controls.Add(txtAlphaTitle);

            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Width = new Unit(1);
            tc.Controls.Add(new LiteralControl("Red\t:"));
            txtRedTitle = new TextBox();
            txtRedTitle.MaxLength = 3;
            txtRedTitle.Width = new Unit(30);


            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Controls.Add(txtRedTitle);

            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Width = new Unit(1);
            tc.Controls.Add(new LiteralControl("Green\t:"));
            txtGreenTitle = new TextBox();
            txtGreenTitle.MaxLength = 3;
            txtGreenTitle.Width = new Unit(30);

            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Controls.Add(txtGreenTitle);

            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Width = new Unit(1);
            tc.Controls.Add(new LiteralControl("Blue\t:"));
            txtBlueTitle = new TextBox();
            txtBlueTitle.MaxLength = 3;
            txtBlueTitle.Width = new Unit(30);

            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Controls.Add(txtBlueTitle);

            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.ColumnSpan = 2;
            tc.Controls.Add(new LiteralControl("Url of Image\t:"));
            txtUrlTitle = new TextBox();


            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.ColumnSpan = 2;
            tc.Controls.Add(txtUrlTitle);

            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Wrap = false;
            tc.Controls.Add(new LiteralControl("Stretch Mode"));

            tc=new TableCell();
            tr.Cells.Add(tc);
            ddlStretchTitle = DropDownListStretch();
            tc.Controls.Add(ddlStretchTitle);

            tbl = new Table();
            this.Controls.Add(tbl);
            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.ColumnSpan = 2;
            tc.Controls.Add(new LiteralControl("<h3>Background for filter panel</h3>"));


            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Width = new Unit(1);
            tc.Controls.Add(new LiteralControl("Alpha\t:"));
            txtAlphaFilter = new TextBox();
            txtAlphaFilter.MaxLength = 3;
            txtAlphaFilter.Width = new Unit(30);

            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Controls.Add(txtAlphaFilter);

            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Width = new Unit(1);
            tc.Controls.Add(new LiteralControl("Red\t:"));
            txtRedFilter = new TextBox();
            txtRedFilter.MaxLength = 3;
            txtRedFilter.Width = new Unit(30);

            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Controls.Add(txtRedFilter);

            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Width = new Unit(1);
            tc.Controls.Add(new LiteralControl("Green\t:"));
            txtGreenFilter = new TextBox();
            txtGreenFilter.MaxLength = 3;
            txtGreenFilter.Width = new Unit(30);

            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Controls.Add(txtGreenFilter);

            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Width = new Unit(1);
            tc.Controls.Add(new LiteralControl("Blue\t:"));
            txtBlueFilter = new TextBox();
            txtBlueFilter.MaxLength = 3;
            txtBlueFilter.Width = new Unit(30);

            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Controls.Add(txtBlueFilter);

            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.ColumnSpan = 2;
            tc.Controls.Add(new LiteralControl("Url of Image\t:"));
            txtUrlFilter = new TextBox();

            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.ColumnSpan = 2;
            tc.Controls.Add(txtUrlFilter);
 
            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Wrap = false;
            tc.Controls.Add(new LiteralControl("Stretch Mode"));

            tc = new TableCell();
            tr.Cells.Add(tc);
            ddlStretchFilter = DropDownListStretch();
            tc.Controls.Add(ddlStretchFilter);

            tbl = new Table();
            this.Controls.Add(tbl);
            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.ColumnSpan = 2;
            tc.Controls.Add(new LiteralControl("<h3>Background for info panel</h3>"));



            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Width = new Unit(1);
            tc.Controls.Add(new LiteralControl("Alpha\t:"));
            txtAlphaInfo = new TextBox();
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Controls.Add(txtAlphaInfo);
            txtAlphaInfo.MaxLength = 3;
            txtAlphaInfo.Width = new Unit(30);


            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Width = new Unit(1);
            tc.Controls.Add(new LiteralControl("Red\t:"));
            txtRedInfo = new TextBox();
            txtRedInfo.MaxLength = 3;
            txtRedInfo.Width = new Unit(30);



            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Controls.Add(txtRedInfo);

            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Width = new Unit(1);
            tc.Controls.Add(new LiteralControl("Green\t:"));
            txtGreenInfo = new TextBox();
            txtGreenInfo.MaxLength = 3;
            txtGreenInfo.Width = new Unit(30);

            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Controls.Add(txtGreenInfo);

            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Width = new Unit(1);
            tc.Controls.Add(new LiteralControl("Blue\t:"));
            txtBlueInfo = new TextBox();
            txtBlueInfo.MaxLength = 3;
            txtBlueInfo.Width = new Unit(30);


            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Controls.Add(txtBlueInfo);

            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.ColumnSpan = 2;
            tc.Controls.Add(new LiteralControl("Url of Image\t:"));
            txtUrlInfo = new TextBox();


            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.ColumnSpan = 2;
            tc.Controls.Add(txtUrlInfo);

            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Wrap = false;
            tc.Controls.Add(new LiteralControl("Stretch Mode"));

            tc = new TableCell();
            tr.Cells.Add(tc);
            ddlStretchInfo = DropDownListStretch();
            tc.Controls.Add(ddlStretchInfo);

            tbl = new Table();
            this.Controls.Add(tbl);
            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.ColumnSpan = 2;
            tc.Controls.Add(new LiteralControl("<h3>Background for content</h3>"));


            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Width = new Unit(1);
            tc.Controls.Add(new LiteralControl("Alpha\t:"));

            txtAlphaContent = new TextBox();
            txtAlphaContent.MaxLength = 3;
            txtAlphaContent.Width = new Unit(30);

            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Controls.Add(txtAlphaContent);



            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Width = new Unit(1);
            tc.Controls.Add(new LiteralControl("Red\t:"));
            txtRedContent = new TextBox();
            txtRedContent.MaxLength = 3;
            txtRedContent.Width = new Unit(30);


            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Controls.Add(txtRedContent);

            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Width = new Unit(1);
            tc.Controls.Add(new LiteralControl("Green\t:"));
            txtGreenContent = new TextBox();
            txtGreenContent.MaxLength = 3;
            txtGreenContent.Width = new Unit(30);


            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Controls.Add(txtGreenContent);

            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Width = new Unit(1);
            tc.Controls.Add(new LiteralControl("Blue\t:"));
            txtBlueContent = new TextBox();
            txtBlueContent.MaxLength = 3;
            txtBlueContent.Width = new Unit(30);


            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Controls.Add(txtBlueContent);

            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.ColumnSpan = 2;
            tc.Controls.Add(new LiteralControl("Url of Image\t:"));
            txtUrlContent = new TextBox();


            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.ColumnSpan = 2;
            tc.Controls.Add(txtUrlContent);

            tr = new TableRow();
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);
            tc.Wrap = false;
            tc.Controls.Add(new LiteralControl("Stretch Mode"));

            tc = new TableCell();
            tr.Cells.Add(tc);
            ddlStretchContent = DropDownListStretch();
            tc.Controls.Add(ddlStretchContent);


            base.CreateChildControls();
        }

        private DropDownList DropDownListStretch()
        {
            DropDownList ddl = new DropDownList();

            ddl.Items.Add(new ListItem("Fill"));
            ddl.Items.Add(new ListItem("None"));
            ddl.Items.Add(new ListItem("Uniform"));
            ddl.Items.Add(new ListItem("Uniform to fill"));

            return ddl;
        }

        public override bool ApplyChanges()
        {
            this.EnsureChildControls();
            PivotViewerWP pivotWP = (PivotViewerWP)this.WebPartToEdit;

            //string[] strARGBTitle = new string[4];
            //strARGBTitle[0] = txtAlphaTitle.Text;
            //strARGBTitle[1] = txtRedTitle.Text;
            //strARGBTitle[2] = txtGreenTitle.Text;
            //strARGBTitle[3] = txtBlueTitle.Text;
            //pivotWP.ARGBTitle = string.Join("|",strARGBTitle);
            //pivotWP.URLTitle = txtUrlTitle.Text;



            //string[] strARGBFilter = new string[4];
            //strARGBFilter[0] = txtAlphaFilter.Text;
            //strARGBFilter[1] = txtRedFilter.Text;
            //strARGBFilter[2] = txtGreenFilter.Text;
            //strARGBFilter[3] = txtBlueFilter.Text;
            //pivotWP.ARGBFilter = string.Join("|",strARGBFilter);
            //pivotWP.URLFilter = txtUrlFilter.Text;


            //string[] strARGBInfo = new string[4];
            //strARGBInfo[0] = txtAlphaInfo.Text;
            //strARGBInfo[1] = txtRedInfo.Text;
            //strARGBInfo[2] = txtGreenInfo.Text;
            //strARGBInfo[3] = txtBlueInfo.Text;
            //pivotWP.ARGBInfo = string.Join("|",strARGBInfo);
            //pivotWP.URLInfo = txtUrlInfo.Text;


            //string[] strARGBContent = new string[4];
            //strARGBContent[0] = txtAlphaContent.Text;
            //strARGBContent[1] = txtRedContent.Text;
            //strARGBContent[2] = txtGreenContent.Text;
            //strARGBContent[3] = txtBlueContent.Text;
            //pivotWP.ARGBContent =string.Join("|", strARGBContent);
            //pivotWP.URLContent = txtUrlContent.Text;

            //pivotWP.HideInfo = chkHideInfoPanel.Checked;
            //pivotWP.HideTitle = chkHideTitleBar.Checked;
            //pivotWP.HideFilter = chkHideFilterPanel.Checked;

            //pivotWP.StretchContent = ddlStretchContent.SelectedValue;
            //pivotWP.StretchTitle= ddlStretchTitle.SelectedValue;
            //pivotWP.StretchInfo= ddlStretchInfo.SelectedValue;
            //pivotWP.StretchFilter= ddlStretchFilter.SelectedValue;

            return true;
        }

        public override void SyncChanges()
        {
            this.EnsureChildControls();
            PivotViewerWP pivotWP = (PivotViewerWP)this.WebPartToEdit;


            //if (pivotWP.ARGBTitle != null)
            //{
            //    string[] ARGBTitle = pivotWP.ARGBTitle.Split('|');
            //    if (ARGBTitle.Length == 4)
            //    {
            //        txtAlphaTitle.Text = ARGBTitle[0];
            //        txtRedTitle.Text = ARGBTitle[1];
            //        txtGreenTitle.Text = ARGBTitle[2];
            //        txtBlueTitle.Text = ARGBTitle[3];
            //    }
            //}
            //else
            //{
            //    txtAlphaTitle.Text = "";
            //    txtRedTitle.Text = "";
            //    txtGreenTitle.Text = "";
            //    txtBlueTitle.Text = "";
            //}


            //txtUrlTitle.Text = pivotWP.URLTitle;



            //if (pivotWP.ARGBFilter != null)
            //{
            //    string[] ARGBFilter = pivotWP.ARGBFilter.Split('|');
            //    if (ARGBFilter.Length == 4)
            //    {
            //        txtAlphaFilter.Text = ARGBFilter[0];
            //        txtRedFilter.Text = ARGBFilter[1];
            //        txtGreenFilter.Text = ARGBFilter[2];
            //        txtBlueFilter.Text = ARGBFilter[3];
            //    }
            //}
            //else
            //{
            //    txtAlphaFilter.Text = "";
            //    txtRedFilter.Text = "";
            //    txtGreenFilter.Text = "";
            //    txtBlueFilter.Text = "";
            //}


            //txtUrlFilter.Text = pivotWP.URLFilter;


            //if (pivotWP.ARGBInfo != null)
            //{
            //    string[] ARGBInfo = pivotWP.ARGBInfo.Split('|');
            //    if (ARGBInfo.Length == 4)
            //    {
            //        txtAlphaInfo.Text = ARGBInfo[0];
            //        txtRedInfo.Text = ARGBInfo[1];
            //        txtGreenInfo.Text = ARGBInfo[2];
            //        txtBlueInfo.Text = ARGBInfo[3];
            //    }
            //}
            //else
            //{
            //    txtAlphaInfo.Text = "";
            //    txtRedInfo.Text = "";
            //    txtGreenInfo.Text = "";
            //    txtBlueInfo.Text = "";
            //}
          


            //txtUrlInfo.Text = pivotWP.URLInfo;


            //if (pivotWP.ARGBContent != null)
            //{
            //    string[] ARGBContent = pivotWP.ARGBContent.Split('|');
            //    if (ARGBContent.Length == 4)
            //    {
            //        txtAlphaContent.Text = ARGBContent[0];
            //        txtRedContent.Text = ARGBContent[1];
            //        txtGreenContent.Text = ARGBContent[2];
            //        txtBlueContent.Text = ARGBContent[3];
            //    }
            //}
            //else
            //{
            //    txtAlphaContent.Text = "";
            //    txtRedContent.Text = "";
            //    txtGreenContent.Text = "";
            //    txtBlueContent.Text = "";
            //}
            

            //txtUrlContent.Text = pivotWP.URLContent;

            //chkHideInfoPanel.Checked = pivotWP.HideInfo;
            //chkHideTitleBar.Checked = pivotWP.HideTitle;
            //chkHideFilterPanel.Checked = pivotWP.HideFilter;


            //ddlStretchContent.SelectedValue = pivotWP.StretchContent;
            //ddlStretchTitle.SelectedValue = pivotWP.StretchTitle;
            //ddlStretchInfo.SelectedValue = pivotWP.StretchInfo;
            //ddlStretchFilter.SelectedValue = pivotWP.StretchFilter;

            return;
        }
    }
}
